<template>
	<view :style="colorStyle">
		<view class='searchGood'>
			<view class='search acea-row row-between-wrapper'>
				<view class='input acea-row row-between-wrapper'>
					<lauwen-select :options="options" :defaultIndex="'2'" :height="45" :padding="10" :fontSize="'1rem'" @getValue="getValue"></lauwen-select>
					<input type='text' v-model='searchValue' @confirm="inputConfirm" focus placeholder='点击搜索商户'
						placeholder-class='placeholder' @input="setValue"></input>
				</view>
				<view class='bnt' @tap='searchBut'>搜索</view>
			</view>

			<view class='line' v-if='merchantList.length'></view>

			<view class='merchantList'>
				<view class='list'>
					<block v-for="(item,index) in merchantList" :key="index">
						<navigator :url="'/pages/merchant/index?mer_id=' + item.id" hover-class='none' class='item acea-row row-between-wrapper'>
							<view class='pictrue'>
								<image :src='item.avatar'></image>
							</view>
							<view class='text acea-row row-column-between'>
								<view class='name line2'>{{item.merchant_name}}</view>
								<view style="margin-bottom: 30px;">地址：{{item.address}}</view>
							</view>
						</navigator>
					</block>
				</view>
			</view>

			<view class='loadingicon acea-row row-center-wrapper' v-if="merchantList.length > 0">
				<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
			</view>
		</view>
		<view class='noCommodity'>
			<view class='pictrue' v-if="merchantList.length == 0">
				<image src='https://fuli-cloud.brdhrm.com/static/images/noSearch.png'></image>
			</view>
			<recommend :hostProduct='hostProduct' v-if="merchantList.length == 0 && page > 1"></recommend>
		</view>
	</view>
</template>

<script>
	import {
		getMerchantList
	} from '@/api/merchant.js';
	import recommend from '@/components/recommend';
	import colors from "@/mixins/color";
	import lauwenSelect from "@/components/lauwen-select/lauwenSelect.vue"
	export default {
		components: {
			recommend,
			lauwenSelect
		},
		mixins: [colors],
		data() {
			return {
				hostProduct: [],
				searchValue: '',
				focus: true,
				merchantList: [],
				first: 0,
				limit: 8,
				page: 1,
				loading: false,
				loadend: false,
				loadTitle: '加载更多',
				hotPage: 1,
				isScroll: true,
				history: [],
				options: {
					'1': "商品",
					'2': "商户",
				}
			};
		},
		onShow: function() {

		},
		onReachBottom: function() {
			if (this.merchantList.length > 0) {
				this.getMerchantList();
			}
		},
		methods: {
			getValue(index) {
				if(index == 1){
					uni.navigateTo({
						url: `/pages/goods_search/index`,
					});
				}
			},
			inputConfirm: function(event) {
				if (event.detail.value) {
					uni.hideKeyboard();
					this.setHotSearchValue(event.detail.value);
				}
			},
			setHotSearchValue: function(event) {
				this.$set(this, 'searchValue', event);
				this.page = 1;
				this.loadend = false;
				this.getMerchantList();
			},
			getMerchantList: function() {
				let that = this;
				if (that.loadend) return;
				if (that.loading) return;
				that.loading = true;
				that.loadTitle = '';
				that.merchantList = [];
				getMerchantList(1,{
					merchant_name: that.searchValue.trim(),
					page: that.page,
					limit: that.limit
				}).then(res => {
					let list = res.data.list,
						loadend = list.length < that.limit;
					that.merchantList = that.$util.SplitArray(list, that.merchantList);
					that.$set(that, 'merchantList', that.merchantList);
					that.loading = false;
					that.loadend = loadend;
					that.loadTitle = loadend ? "人家是有底线的~" : "加载更多";
					that.page = that.page + 1;
				}).catch(err => {
					that.loading = false,
						that.loadTitle = '加载更多'
				});
			},
			setValue: function(event) {
				this.$set(this, 'searchValue', event.detail.value);
			},
			searchBut: function() {
				let that = this;
				if (!that.searchValue.trim()) return this.$util.Tips({
					title: '请输入要搜索的商户'
				});
				that.focus = false;
				// if (that.searchValue.length > 0) {
				that.page = 1;
				that.loadend = false;
				that.$set(that, 'merchantList', []);
				uni.showLoading({
					title: '正在搜索中'
				});
				that.getMerchantList();
				uni.hideLoading();
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff !important;
	}

	.noCommodity {
		border-top-width: 0;
	}

	.searchGood .search {
		padding-left: 30rpx;
	}

	.searchGood .search {
		margin-top: 20rpx;
	}

	.searchGood .search .input {
		width: 598rpx;
		background-color: #f7f7f7;
		border-radius: 33rpx;
		padding: 0 35rpx;
		box-sizing: border-box;
		height: 66rpx;
	}

	.searchGood .search .input input {
		width: 380rpx;
		font-size: 28rpx;
	}

	.searchGood .search .input .placeholder {
		color: #999;
	}

	.searchGood .search .input .iconfont {
		color: #555;
		font-size: 35rpx;
	}

	.searchGood .search .bnt {
		width: 120rpx;
		text-align: center;
		height: 66rpx;
		line-height: 66rpx;
		font-size: 30rpx;
		color: #282828;
	}

	.searchGood .title {
		font-size: 28rpx;
		color: #999;
		margin: 50rpx 30rpx 25rpx 30rpx;
	}

	.searchGood .title .iconfont {
		font-size: 28rpx;
	}

	.searchGood .line {
		border-bottom: 1rpx solid #eee;
		margin: 20rpx 30rpx 0 30rpx;
	}

	.merchantList .list {
		margin-top: 20rpx;
	}

	.merchantList .list .item {
		margin: 0 30rpx;
		border-bottom: 1rpx solid #f0f0f0;
		padding: 35rpx 0;
	}

	.merchantList .list .item .pictrue {
		width: 150rpx;
		height: 150rpx;
	}

	.merchantList .list .item .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 6rpx;
	}

	.merchantList .list .item .text {
		width: 480rpx;
		height: 156rpx;
		font-size: 24rpx;
		color: #999;
	}

	.merchantList .list .item .text .name {
		font-size: 33rpx;
		color: #282828;
		margin-top: 8px;
	}

	.merchantList .list .item .picList .pictrue {
		width: 335rpx;
		height: 210rpx;
		margin-top: 30rpx;
	}

	.merchantList .list .item .picList.on .pictrue {
		width: 217rpx;
		height: 136rpx;
	}

	.merchantList .list .item .picList .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 6rpx;
	}

	.merchantList .list .item .time {
		text-align: right;
		font-size: 24rpx;
		color: #999;
		margin-top: 22rpx;
	}
</style>
